package likou;

public class _714 {
    public static void main(String[] args) {
        System.out.println(maxProfit2(new int[]{1, 3, 2, 8, 4, 9}, 2));
    }

    public static int maxProfit(int[] prices, int fee) {
        if (prices.length == 0) return 0;
        int res = 0;
        int p = fee + prices[0];
        for (int i : prices) {
            if (i > p) {
                res += i - p;
            } else if (p > i + fee) {
                p = i + fee;
            }
        }
        return res;
    }

    public static int maxProfit2(int[] prices, int fee) {
        int buy = prices[0] + fee;//代价
        int res = 0;
        for (int price : prices) {
            //代价更小，则
            if (price + fee < buy) {
                buy = price + fee;
            } else if (price > buy) {
                res += price - buy;
                buy = price;//下次购买代价的底线，就是今天卖出的价格
            }
        }
        return res;
    }public int maxProfit(int[] prices) {
        if(prices.length<2) return 0;
        int min = prices[0];
        int res = 0;
        for(int price:prices){
            if(min>=price){
                min = price;
                continue;
            }
            //每次直接得到最优解二
            res = Math.max(price-min,res);
        }
        return res;
    }
}
